﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Query.SiteModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Facade.SiteModule.DataTransferObject;
using Resto.CostControl.Core.Cache;
using Enterprise.Framework.Frame.Architecture;

namespace Resto.CostControl.Core.Facade.SiteModule.FacadeObject
{
    /// <summary>
    /// 项目点
    /// </summary>
    public class SiteFO : IApplicationFacadeObject
    {
        #region 私有属性

        private SiteDQO _SiteDQO;
        private SiteDQO SiteDQO
        {
            get
            {
                if (_SiteDQO == null)
                    _SiteDQO = ObjectCreator.Create<SiteDQO>();

                return _SiteDQO;
            }
        }

        #endregion

        #region 公共方法

        public virtual void AddSite(SiteDTO siteDTO)
        {
            Site site = siteDTO.ToSite();
            site.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            siteDTO.ID = site.ID;
        }

        public virtual void UpdateSite(SiteDTO siteDTO)
        {
            Site site = SiteDQO.GetSite(siteDTO.ID);
            siteDTO.CopyToSite(site);

            RestoCostDomainSession.CurrentSession.SaveChanges();

            SiteDTOCache.Remove(site.ID);
        }

        public virtual void DeleteSite(string id)
        {
            Site site = SiteDQO.GetSite(id);

            site.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            SiteDTOCache.Remove(site.ID);
        }

        public virtual SiteDTO GetSiteDTO(string id)
        {
            Site site = SiteDQO.GetSite(id);
            if (site.IsDelete)
                return null;

            return new SiteDTO(site);
        }

        #endregion
    }
}
